<?php 

if ($_GET['faction'] == 'actionsort'){
	$vSettings = AFT_load('settings');	
	$vactn=$_GET['actn'];
	$vSettings['sort']=$vactn;
	AFT_save($vSettings, 'settings');
}
if ($_GET['faction'] == 'actionorderby'){
	$vSettings = AFT_load('settings');	
	$vactn=$_GET['actn'];
	$vSettings['orderby']=$vactn;
	AFT_save($vSettings, 'settings');
}

if ($_GET['faction'] == 'Seedunk'
	|| $_GET['faction'] == 'Seedunkcnt'
	|| $_GET['faction'] == 'Seedplacecnt'
	|| $_GET['faction'] == 'Seedwater'
	) {
	$vSettings = AFT_load('settings');	
	$vSettings[$_GET['faction']]=$_GET['val'];
	AFT_save($vSettings, 'settings');
}

if ($_GET['faction'] == 'actionharvestorc'){
	$vSettings = AFT_load('settings');
	if ($_GET['harvestorc']=='ok') {
		$vSettings['harvestorc']=true;
	} else {
		$vSettings['harvestorc']=false;
	}
	AFT_save($vSettings, 'settings');
}

//trees
if ($_GET['faction'] == 'actioncnt'
	|| $_GET['faction'] == 'action'
	|| $_GET['faction'] == 'actiontransp'
	|| $_GET['faction'] == 'actiontranspcnt'
	|| $_GET['faction'] == 'actionGB'
	|| $_GET['faction'] == 'actionGBcnt'
	|| $_GET['faction'] == 'actionST'
	|| $_GET['faction'] == 'actionSTcnt'
	|| $_GET['faction'] == 'actionSeed'
	|| $_GET['faction'] == 'actionSeedcnt'
	|| $_GET['faction'] == 'actionBred'
	|| $_GET['faction'] == 'actionBredcnt'
	){
	$vAllTrees = AFT_load('alltrees');	
	$vAllTrees[$_GET['code']][$_GET['faction']]=$_GET['val'];
	AFT_save($vAllTrees, 'alltrees');
}
if ($_GET['faction'] == 'movetosec'
	|| $_GET['faction'] == 'movetoorc'){
	$vAllTrees = AFT_load('alltrees');	
	$vAllTrees[$_GET['code']]['movetosec'] = 0;
	$vAllTrees[$_GET['code']]['movetoorc'] = 0;
	$vAllTrees[$_GET['code']][$_GET['faction']] = $_GET['val'];
	AFT_save($vAllTrees, 'alltrees');
}

//orchards
if ($_GET['faction'] == 'setorc'){
	$vOrchards = AFT_load('orchards');	
	$void=$_GET['oid'];
	$vtid=$_GET['tid'];
	$vcnt=$_GET['cnt'];
	for ($i=0; $i<count($vOrchards['oid']) ; $i++) {
		if ($vOrchards['oid'][$i]==$void && $vOrchards['tid'][$i]==$vtid) {
			$vOrchards['cnt'][$i]=$vcnt;
		}
	}
	AFT_save($vOrchards, 'orchards');
}

if ($_GET['faction'] == 'actionshowall'){
	$vSettings = AFT_load('settings');
	if ($_GET['showalltrees']=='ok') {
		$vSettings['showalltrees']=true;
	} else {
		$vSettings['showalltrees']=false;
	}
	AFT_save($vSettings, 'settings');
}

// GENERAL ACTIONS

if ($_GET['faction'] == 'general'){
	$vAllTrees = AFT_load('alltrees');	
	if ($_GET['actn'] == 'clearallgb'){
		foreach ($vAllTrees as &$t) {
			$t['actionGB']='select';
			$t['actionGBcnt']=0;
		}
	}
	if ($_GET['actn'] == 'placeallgb'){
		foreach ($vAllTrees as &$t) {
			$t['actionGB']='place';
			$t['actionGBcnt']=0;
		}
	}

	if ($_GET['actn'] == 'placeallugb'){
		foreach ($vAllTrees as &$t) {
			if ($t['mastery'] < 1) {
				$t['actionGB']='place';
				$t['actionGBcnt']=0;
			}
		}
	}

	if ($_GET['actn'] == 'clearallst'){
		foreach ($vAllTrees as &$t) {
			$t['actionST']='select';
			$t['actionSTcnt']=0;
		}
	}
	if ($_GET['actn'] == 'placeallst'){
		foreach ($vAllTrees as &$t) {
			$t['actionST']='place';
			$t['actionSTcnt']=0;
		}
	}
	if ($_GET['actn'] == 'sellallst'){
		foreach ($vAllTrees as &$t) {
			$t['actionST']='sell';
			$t['actionSTcnt']=0;
		}
	}

	if ($_GET['actn'] == 'clearallseed'){
		foreach ($vAllTrees as &$t) {
			$t['actionSeed']='select';
			$t['actionSeedcnt']=0;
		}
	}
	if ($_GET['actn'] == 'waterallseed'){
		foreach ($vAllTrees as &$t) {
			$t['actionSeed']='water';
			$t['actionSeedcnt']=$_GET['waterallseedcnt'] ?: -1;
		}
	}
	if ($_GET['actn'] == 'wateralluseed'){
		foreach ($vAllTrees as &$t) {
			if ($t['mastery'] < 1) {
				$t['actionSeed']='water';
				$t['actionSeedcnt'] = $_GET['wateralluseedcnt'] ?: 19;
			}
		}
	}
	if ($_GET['actn'] == 'sellallseed'){
		foreach ($vAllTrees as &$t) {
			$t['actionSeed']='sell';
			$t['actionSeedcnt']=0;
		}
	}	
	if ($_GET['actn'] == 'clearallbred'){
		foreach ($vAllTrees as &$t) {
			$t['actionBred']='select';
			$t['actionBredcnt']=0;
		}
	}
	if ($_GET['actn'] == 'bredmastery'){
		foreach ($vAllTrees as &$t) {
			$t['actionBred']='tomast';
			$t['actionBredcnt']=0;
		}
	}
	if ($_GET['actn'] == 'bredcoins'){
		foreach ($vAllTrees as $vATr) {
			$vatmp[]=$vATr['coins'];
		}
		array_multisort($vatmp, SORT_DESC, $vAllTrees); 
		$num=0;
		foreach ($vAllTrees as &$t) {
			if ($t['cnt']>0 && $num<20) {
				$num=$num+$t['cnt'];
				$t['actionBred']='forced';
				$t['actionBredcnt']=0;
			} else {
				$t['actionBred']='select';
				$t['actionBredcnt']=0;
			}
		}
		AFT_save($vAllTrees, 'alltrees');
	}
	if ($_GET['actn'] == 'bredcoconuts'){
		foreach ($vAllTrees as $vATr) {
			$vatmp[]=$vATr['cocos'];
		}
		array_multisort($vatmp, SORT_DESC, $vAllTrees); 
		$num=0;
		foreach ($vAllTrees as &$t) {
			if ($t['cnt']>0 && $num<20) {
				$num=$num+$t['cnt'];
				$t['actionBred']='forced';
				$t['actionBredcnt']=0;
			} else {
				$t['actionBred']='select';
				$t['actionBredcnt']=0;
			}
		}
	}

	if ($_GET['actn'] == 'clearallorc'){
		foreach ($vAllTrees as &$t) {
			$t['movetoorc']=0;
		}
	}
	if ($_GET['actn'] == 'clearallsec'){
		foreach ($vAllTrees as &$t) {
			$t['movetosec']=0;
		}
	}
	if ($_GET['actn'] == 'clearallremorc'){
		$objects  = @unserialize(file_get_contents(F('objects.txt')));
		$vOrchards = array();
		foreach ($objects as $oID => $o) {
			if ($o['className']=='OrchardBuilding') {
				foreach ($o['contents'] as $conts) {
					$vrcnt=0;
					$vOrchards['oid'][]=$o['id'];	
					$vOrchards['tid'][]=Units_GetNameByCode($conts['itemCode']);
					$vOrchards['cnt'][]=0;
				}
			}
		}
		AFT_save($vOrchards, 'orchards');
	}
	if ($_GET['actn'] == 'orcfree'){
		$objects  = @unserialize(file_get_contents(F('objects.txt')));
		$vOrchards = array();
		foreach ($objects as $oID => $o) {
			if ($o['className']=='OrchardBuilding') {
				foreach ($o['contents'] as $conts) {
					$vOrchards['oid'][]=$o['id'];	
					$vOrchards['tid'][]=Units_GetNameByCode($conts['itemCode']);
					$vOrchards['cnt'][]=$conts['numItem'];
				}
			}
		}
		AFT_save($vOrchards, 'orchards');
	}
	if ($_GET['actn'] == 'clearallsell'){
		foreach ($vAllTrees as &$t) {
			$t['action']='select';
			$t['actioncnt']=0;
		}
	}
	
	if ($_GET['actn'] == 'clearalltransp'){
		foreach ($vAllTrees as &$t) {
			$t['actiontransp']='0';
			$t['actiontranspcnt']=0;
		}
	}
	
	AFT_save($vAllTrees, 'alltrees');
} // General actions

if ($_GET['faction'] == 'sellalltrees'){
	$vSettings = AFT_load('settings');
	$vAllTrees = AFT_load('alltrees');	
	foreach ($vAllTrees as &$t) {
		if ($_GET['keepprecious']) {
			if ($t['coins'] >= $vSettings['preciousCoins']) continue;
			if ($t['cocos'] >= $vSettings['preciousCocos']) continue;
			if ($t['jade'] >= $vSettings['preciousJade']) continue;
		}
		if ($_GET['keepunmastered'] && $t['mastery'] < 1) continue;
		$t['action']='sell';
		$t['actioncnt']=$_GET['keep'];
	}
	AFT_save($vAllTrees, 'alltrees');
}

if ($_GET['faction'] == 'malltosec'){
	$vsect=$_GET['sec'];
	$vAllTrees = AFT_load('alltrees');	
	foreach ($vAllTrees as &$t) {
		$t['movetoorc']=0;
		$t['movetosec']=$vsect;
	}
	AFT_save($vAllTrees, 'alltrees');
}
if ($_GET['faction'] == 'mothtosec'){
	$vsect=$_GET['sec'];
	$vAllTrees = AFT_load('alltrees');	
	foreach ($vAllTrees as &$t) {
		if ($t['movetosec']==0) {
			$t['movetoorc']=0;
			$t['movetosec']=$vsect;
		}
	}
	AFT_save($vAllTrees, 'alltrees');
}
if ($_GET['faction'] == 'trkeeptrees'){
	$vkeep=$_GET['keep'];
	$vAllTrees = AFT_load('alltrees');	
	foreach ($vAllTrees as &$t) {
		$t['actiontranspcnt']=$vkeep;
	}
	AFT_save($vAllTrees, 'alltrees');
}
if ($_GET['faction'] == 'talltofarm'){
	$vfarm=$_GET['farm'];
	$vAllTrees = AFT_load('alltrees');	
	foreach ($vAllTrees as &$t) {
		$t['actiontransp']=$vfarm;
	}
	AFT_save($vAllTrees, 'alltrees');
}

 ?>
 ok
 